package com.throttle.raildroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class HelpActivity extends Activity {
	SeekBar sb;
	TextView trainSpeedText;
	int trainNumber;
	boolean controlled;
	boolean selected = false;
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
	    setContentView(R.layout.grid);
	    sb = (SeekBar) findViewById(R.id.trainSpeedSetter);
	    trainSpeedText = (TextView) findViewById(R.id.trainSpeedText);
	    RailDroidActivity ra = (RailDroidActivity) getParent();
	    trainNumber = ra.tabHost.getCurrentTab()-1;
	    
	    final Button button = (Button) findViewById(R.id.getTrain);
	    final ToggleButton lightButton = (ToggleButton) findViewById(R.id.light);
	    lightButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(controlled)
				{
					//boolean success = StaticLogList.throttle.setTrainSpeed(trainNumber, progress*10);
				}
			}
		});
	    
	    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub 
				
			} 
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if(controlled)
				{
					boolean success = StaticLogList.throttle.setTrainSpeed(trainNumber, progress*10);
					if(success)
					{
						trainSpeedText.setText("TrainSpeed:"+Integer.toString(progress));
						StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Train number "+Integer.toString(trainNumber)+"'s speed set to "+Integer.toString(progress)+"."));
						return;
					}
					Toast.makeText(HelpActivity.this, "Failed to set train's speed.", Toast.LENGTH_SHORT).show();
					StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Failed to set train number "+Integer.toString(trainNumber)+"'s speed."));
					seekBar.setProgress(0);
					return;
				}
				seekBar.setProgress(0);
				Toast.makeText(HelpActivity.this, "Train is not controlled.", Toast.LENGTH_SHORT).show();
			}
		});
	    
	    button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				boolean success = StaticLogList.throttle.controlTrain(trainNumber);
				if(success)
				{
					controlled = true;
					Toast.makeText(HelpActivity.this, "Controlled.", Toast.LENGTH_SHORT).show();
					StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Train "+Integer.toString(trainNumber)+" controlled."));
					return;
				}
				StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Failed to controll train number "+Integer.toString(trainNumber)+"."));
				Toast.makeText(HelpActivity.this, "Failed to control.", Toast.LENGTH_SHORT).show();
			}
		});
	    
	    
    }
}